//
//  UserCollectViewController.m
//  TeacherNet
//
//  Created by slz on 2017/12/20.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "UserCollectViewController.h"
#import "CourseTableViewCell.h"
#import "CourseDetailViewController.h"


@implementation UserCollectModel
+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{@"collectList" : [CourseModel class],
             };
}

+ (NSDictionary *)modelCustomPropertyMapper {
    return @{@"collectList" : @"userCollectList"
             };
}
@end

@interface UserCollectViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) YWTableView *tableView;
@property (nonatomic,strong) UserCollectModel *model;
@end

@implementation UserCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.showNavigationBar = YES;
    [self setControllerTitle:@"我的收藏"];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"back_icon" selectImage:nil addTarget:self andAction:@selector(backAction)];
    
    @weakify(self);
    _tableView = [[YWTableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        [self regreshData];
    }];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.estimatedRowHeight = 80;
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    [_tableView.mj_header beginRefreshing];
    // Do any additional setup after loading the view.
}

#pragma mark - UITbaleViewDelegate


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * reuserIndentifier = @"CourseTableViewCell";
    CourseTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuserIndentifier];
    if (!cell){
        cell = [[CourseTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuserIndentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    CourseModel * model = _model.collectList[indexPath.row];
    [cell layoutWithModel:model];
    return cell;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _model.collectList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CourseModel * model = _model.collectList[indexPath.row];
    if (model.courseId && model.courseId.length > 0){
        CourseDetailViewController * detailController = [CourseDetailViewController new];
        detailController.courseId = model.courseId;
        [self.navigationController pushViewController:detailController animated:YES];
    }
}

- (void)regreshData{
    [BaseHttpTool get:kAPI_USER_COLLECT params:@{@"user_id":[MFWSdkEnvironment sharedInstance].userInfo.uid} success:^(id responseObj) {
        [_tableView.mj_header endRefreshing];
        NSDictionary * dict = [[MFWJson jsonWithObject:responseObj] toDic];
        _model = [UserCollectModel yy_modelWithDictionary:dict];
        [_tableView  reloadData];
    } failure:^(NSError *error) {
        [_tableView.mj_header endRefreshing];

    }];
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
